package com.qq.BFMRSE.frame;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;

import com.qq.BFMRSE.encrypt.desEncrypt;
import com.qq.BFMRSE.entity.reposeParam;
import com.qq.BFMRSE.extractWord.SelectHelper;
import com.qq.BFMRSE.extractWord.SelectWord;
import com.qq.BFMRSE.extractWord.wordTFAndIDFInfo;
import com.qq.BFMRSE.requestModule.addFileModule;
import com.qq.BFMRSE.requestModule.deleteFileModule;
import com.qq.BFMRSE.requestModule.upLoadFileModule;
import com.qq.BFMRSE.serialize.objectSerializeUtil;
import com.qq.BFMRSE.util.BFConstants;

public class updateFrame extends JFrame implements ActionListener{
private JLabel addLabel;
private JTextField addTF;
private JButton updateBtn ;
private JButton addfileChoseBtn;
private JFileChooser jfc;
private JRadioButton deleteJrbtn;
private JRadioButton addJrbtn;
private ButtonGroup bg;
private JTextField deletTF;
private JLabel deleteLabel;
private int UT;//更新操作类型
private String userName;
/**
 * Create the frame.
 */
public updateFrame(String userName) {
	init();
	UT=-1;
	this.userName=userName;
	this.setTitle("更新操作界面");
    ImageIcon imgBF=new ImageIcon("images\\BFMRSE.png");
    this.setIconImage(imgBF.getImage()); 
	getContentPane().setLayout(null);
	jfc.setCurrentDirectory(new File("d://"));
	setSize(500, 300);
	setVisible(true);
	setResizable(true);// 禁止拉边框拉长拉断
	setLocationRelativeTo(null);
}
public void init(){
	addLabel = new JLabel("添加文件路径：");
	addTF = new JTextField(10);
	updateBtn = new JButton("执行更新操作");
	updateBtn.addActionListener(this);
	addLabel.setBounds(79, 60, 114, 30);
	getContentPane().add(addLabel);
	addTF.setBounds(169, 63, 135, 25);
	getContentPane().add(addTF);
	updateBtn.setBounds(79, 172, 299, 30);
	getContentPane().add(updateBtn);
	jfc = new JFileChooser();
	
	addfileChoseBtn = new JButton("选择");
	addfileChoseBtn.setBounds(314, 63, 64, 25);
	getContentPane().add(addfileChoseBtn);
	addfileChoseBtn.addActionListener(this);
	deleteJrbtn = new JRadioButton("删除文件");
	deleteJrbtn.setBounds(99, 20, 121, 23);
	getContentPane().add(deleteJrbtn);
	deleteJrbtn.addActionListener(this);
	addJrbtn = new JRadioButton("添加文件");
	addJrbtn.setBounds(238, 20, 121, 23);
	getContentPane().add(addJrbtn);
	addJrbtn.addActionListener(this);
	bg=new ButtonGroup();
	bg.add(deleteJrbtn);
	bg.add(addJrbtn);
	deletTF = new JTextField(10);
	deletTF.setBounds(169, 117, 135, 25);
	getContentPane().add(deletTF);
	deleteLabel = new JLabel("删除文件名称：");
	deleteLabel.setBounds(79, 114, 114, 30);
	getContentPane().add(deleteLabel);
}
/**
 * Launch the application.
 */
public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
		public void run() {
			try {
				String userName="yuan";
				updateFrame frame = new updateFrame(userName);
				frame.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	});
}
@Override
public void actionPerformed(ActionEvent e) {
	// TODO Auto-generated method stub
	if(e.getSource()==addfileChoseBtn){
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
		if (state == 1) {
			return;// 撤销则返回
		} else {
			File skFile=jfc.getSelectedFile();
			addTF.setText(skFile.getAbsolutePath());
		}
	}else if(e.getSource()==deleteJrbtn){
		//删除文件操作
		addTF.setEnabled(false);
		addfileChoseBtn.setEnabled(false);//使得添加文件功能无法使用
		UT=BFConstants.UT_DELETE;
		deletTF.setEnabled(true);
		
	}else if(e.getSource()==addJrbtn){
		//添加文件操作
		deletTF.setEnabled(false);//使得删除文件功能无法使用
		UT=BFConstants.UT_ADD;
		addTF.setEnabled(true);
		addfileChoseBtn.setEnabled(true);
	}
	else if(e.getSource()==updateBtn){
		//进行更新操作
		String wordInfoFilePath=BFConstants.WORDINFOFILE+File.separator+BFConstants.INITKEYVALUE;
		SelectWord sWord=null;
		try {
			sWord=(SelectWord) objectSerializeUtil.deserialize(wordInfoFilePath);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(UT==BFConstants.UT_DELETE){
			String deleteFileName=deletTF.getText();
			if(wordTFAndIDFInfo.isFileExist(deleteFileName)){
				if(sWord!=null){
					deleteFileModule df=new deleteFileModule(userName,deleteFileName,sWord.getIndex());
					String deleteUrl=BFConstants.URL+BFConstants.URLSPE+BFConstants.DELETEFILE;
					try {
						reposeParam resParam=df.deleteFile(deleteUrl);
						if(resParam.isFlag())
						{
							if(resParam.getStateCode()==200)
							{
								//System.out.println("文件删除操作完成！");
								JOptionPane.showMessageDialog(null, "文件删除操作完成！");
							}
							else {
								JOptionPane.showMessageDialog(null, "文件删除发生问题，问题为："+resParam.getStateCode()+"！");
							}
						}
						else {
							JOptionPane.showMessageDialog(null, "文件在服务器中不存在！");
						}
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}else {
					JOptionPane.showMessageDialog(null, "用户配置信息读取有误！");
				}
				
				
			}else{
				JOptionPane.showMessageDialog(null, "文件"+deleteFileName+"不存在，请重新输入要删除文件名称！");
			}
			
			
		}else if(UT==BFConstants.UT_ADD){
			String addNewFilePath=addTF.getText();
			String fileName=SelectHelper.getReFileName(addNewFilePath);
			if(!wordTFAndIDFInfo.isFileExist(fileName)){
				if(sWord!=null){
					addFileModule am=new addFileModule(userName, addNewFilePath, sWord.getIndex(), sWord.getSkPath());
					String addUrl=BFConstants.URL+BFConstants.URLSPE+BFConstants.ADDFILE;
					reposeParam retParam=am.addFile(addUrl);
					if(retParam.isFlag())
					{
						if(retParam.getStateCode()==200)
						{
							//上传加密文件
							String upFileUrl=BFConstants.URL+BFConstants.URLSPE+BFConstants.UPENCRYPTFILEURL;//上传加密文件url
							int uFstateCode=am.upNewFile(upFileUrl);
							if(uFstateCode==200){
								JOptionPane.showMessageDialog(this, "文件添加操作完成！",
										"提示",JOptionPane.INFORMATION_MESSAGE);
							}else{
								JOptionPane.showMessageDialog(this, "索引更新完成，文件上传操作失败！",
										"提示",JOptionPane.INFORMATION_MESSAGE);
							}
						}
						else {
							JOptionPane.showMessageDialog(this, "服务器或者网络部分发生问题，问题为："+retParam.getStateCode()+"！",
									"提示",JOptionPane.INFORMATION_MESSAGE);
						}
					}
					else {
						JOptionPane.showMessageDialog(this, "客户端部分发生问题！",
								"提示",JOptionPane.INFORMATION_MESSAGE);
					}
					
				}else {
					JOptionPane.showMessageDialog(this, "用户配置信息读取有误！",
							"提示",JOptionPane.INFORMATION_MESSAGE);
					
				}
				
				
				
			}else{
				JOptionPane.showMessageDialog(this, "文件"+addNewFilePath+"已经存在！",
						"提示",JOptionPane.INFORMATION_MESSAGE);
			}
		}
	} 
}
}
